#include <config.h>
#include <header.h>
#include <debug.h>
#include <libwnck/libwnck.h>

#include "gui.h"

char *desktop_entry_dir[] =
{
  "/usr/share/applications/",
  "/usr/local/share/applications/",
};

char *app_icon_dir[] =
{
  "/usr/share/pixmaps/",
  "/usr/share/app-install/icons/",
  "/usr/share/icons/hicolor/32x32/apps/",
  "/usr/local/share/icons/hicolor/32x32/apps/",
};

static void chomp(char *line);

GdkPixbuf* get_program_icon_by_pid(pid_t pid)
{
  GList *tmp = NULL;
  WnckScreen *screen = NULL;
  GdkPixbuf *program_icon = NULL;
  WnckWindow *window = NULL;
  int w_pid = 0;
  int i = 0;

  __GUI_DEBUG("enter %s\n", __FUNCTION__);

  screen = wnck_screen_get_default();
  if (NULL == screen)
  {
    __ERROR_MSG("can't get screen!\n");
    return NULL;
  }

  wnck_screen_force_update(screen);

  tmp = wnck_screen_get_windows(screen);
  if (NULL == tmp)
  {
    __ERROR_MSG("can't get window!\n");
    return NULL;
  }

  while (tmp != NULL)
  {
    window = tmp->data;
    w_pid = wnck_window_get_pid(window);

    if (pid == (pid_t)w_pid)
    {
      program_icon = wnck_window_get_icon(window);
      break;
    }

    tmp = tmp->next;
  }

  return program_icon;
}

GdkPixbuf* get_program_icon_by_desktop_entry(char *app_name)
{
  int i = 0;
  int is_find_desktop_entry = 0;
  char entry_path[256] = {0};
  char icon_path[256] = {0};
  char icon_name[64] = {0};
  char app_treated_name[64] ={0};
  char line[513] = {0};
  FILE *fp = NULL;
  char *temp = NULL;
  char *temp_treated = NULL;
  GdkPixbuf *pixbuf = NULL;

  /*find desktop entry first*/
  for (i = 0; i < sizeof(desktop_entry_dir) / sizeof(desktop_entry_dir[0]); i ++)
  {
    sprintf(entry_path,  "%s%s.desktop", desktop_entry_dir[i], app_name);

    if (0 == access(entry_path, 0))
    {
      __GUI_DEBUG("find desktop entry:%s\n", entry_path);
      /*get icon name*/
      is_find_desktop_entry = 1;
      fp = fopen(entry_path, "r");
      if (NULL == fp)
      {
        __ERROR_MSG("error when open %s\n",  entry_path);
        goto out;
      }

      while (fgets(line, 512, fp))
      {
        if (0 == strncmp("Icon=", line, 4))
        {
          strcpy(icon_name, line + 5);
          chomp(icon_name);
          break;
        }

        memset(line, 0, sizeof(line));
      }

      break;
    }

    memset(entry_path, 0, sizeof(entry_path));
  }

  if (0 == is_find_desktop_entry)
  {
    temp = app_name;
    temp_treated = app_treated_name;

    while(*temp)
    {
      if (isalnum(*temp))
      {
        *temp_treated = *temp;
      }
      else
      {
        break;
      }

      temp++;
      temp_treated++;
    }

    __GUI_DEBUG("the app name after treated:%s\n", app_treated_name);

    for (i = 0; i < sizeof(desktop_entry_dir) / sizeof(desktop_entry_dir[0]); i++)
    {
      sprintf(entry_path, "%s%s.desktop", desktop_entry_dir[i], app_treated_name);

      if (0 == access(entry_path, 0))
      {
        __GUI_DEBUG("find desktop entry:%s\n", entry_path);
        /*get icon name*/
        is_find_desktop_entry = 1;
        fp = fopen(entry_path, "r");
        if (NULL == fp)
        {
          __ERROR_MSG("error when open %s\n", entry_path);
          goto out;
        }

        while (fgets(line, 512, fp))
        {
          if (0 == strncmp("Icon=", line, 4))
          {
            strcpy(icon_name,  line + 5);
            chomp(icon_name);
            break;
          }

          memset(line, 0, sizeof(line));
        }

        break;
      }

      memset(entry_path, 0, sizeof(entry_path));
    }
  }

  if (0 == is_find_desktop_entry)
  {
    __GUI_DEBUG("can't find desktop entry:%s\n", entry_path);
    goto out;
  }

  if (!strstr(icon_name, ".png"))
  {
    strcat(icon_name, ".png");
  }

  /*find icon in possible icon path*/
  for (i = 0; i < sizeof(app_icon_dir) / sizeof(app_icon_dir[0]); i++)
  {
    sprintf(icon_path, "%s%s", app_icon_dir[i], icon_name);
    __DEBUG_MSG("icon path:%s\n", icon_path);
    if (0 == access(icon_path, 0))
    {
      pixbuf = gdk_pixbuf_new_from_file(icon_path, NULL);
      break;
    }
  }

  out:
  if (fp != NULL)
  {
    fclose(fp);
  }

  return pixbuf;
}

static void chomp(char *line)
{
  int i = 0;
  if (line == NULL)
  {
    __ERROR_MSG("wrong parameter\n");
    return;
  }

  i = strlen(line);

  *(line + i - 1) = 0;
  return;
}
